mod config;
mod controller;
mod dao;
mod global_var;
mod models;
mod service;
mod tasks;
mod utils;

pub use config::*;
pub use controller::*;
pub use global_var::*;
pub use models::*;
pub use tasks::*;
use tauri::Manager;
pub use utils::*;

use std::sync::{Arc, Mutex};
use std::thread;

pub fn run() {
    tauri::Builder::default()
        .plugin(tauri_plugin_opener::init())
        .setup(move |app| {
            let queue = GLOBAL_QUEUE.clone();
            let app_handle = app.handle().clone();
            let config = create_message_queue_server_config(app_handle); // 4 个线程
                                                                         // 创建服务端
            let server = Arc::new(Mutex::new(Server::new(Arc::clone(&queue), config)));
            // 启动服务端线程
            let _server_thread = Arc::new(Mutex::new(Some(thread::spawn({
                let server = Arc::clone(&server);
                move || {
                    let server = server.lock().unwrap();
                    server.start();
                }
            }))));
            let splash_window = app.get_webview_window("splash").unwrap();
            let main_window = app.get_webview_window("main").unwrap();
            std::thread::spawn(move || {
                std::thread::sleep(std::time::Duration::from_secs(3)); // 模拟 3 秒加载时间
                splash_window.close().unwrap(); // 关闭启动窗口
                main_window.show().unwrap(); // 显示主窗口
            });

            Ok(())
        })
        .invoke_handler(tauri::generate_handler![run_playwright])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
